function showUser() {
    let userNaw = JSON.parse(localStorage.getItem('userNaw')) || {}
    if (userNaw) {
        $('.outName').html(`用户名：<a href="#">${userNaw.username}</a>|
        <a href="http://127.0.0.1:5555/%E7%86%8A%E7%8C%AB%E4%BC%98%E9%80%89/src/pages/loading.html">注销</a>`)
    } else {
        $('.outName').html(`<a href="http://127.0.0.1:5555/%E7%86%8A%E7%8C%AB%E4%BC%98%E9%80%89/src/pages/loading.html">登录|注册</a>`)
    }
}
showUser()
//点击图片跳转首页
function turn() {
    location.href = `http://127.0.0.1:5555/%E7%86%8A%E7%8C%AB%E4%BC%98%E9%80%89/src/pages/index.html`
}
let spanEle = document.querySelector('.total-price')
let tableEle = document.querySelector('table')
let nameEle = document.querySelector('input[name="name"]')
let urlEle = document.querySelector('input[name="url"]')
let priceEle = document.querySelector('input[name="price"]')
let btnEle = document.querySelector('input[value="新增商品"]')                            
let checkall=false
function fu() {
    var cart1= localStorage.getItem('cart')
    cartArray=JSON.parse(cart1)||[]
    let m = `
    <tr>
               <th><input type='checkbox' class='box' value='check-all' ${checkall?'checked':''}></th>
               <th>商品号</th>
               <th>商品名</th>
               <th>图片</td>
               <th>单价</th>
               <th>数量</th>
               <th>总价</th>
               <th>操作</th>
   </tr>`

    for (var i = 0; i < cartArray.length; i++) {

        m = m + `
           <tr>
                <td><input type='checkbox' value='check' ${cartArray[i].state?'checked':''}></td>
               <td>${cartArray[i].id}</td>
               <td>${cartArray[i].name}</td>
               <td><image src=${cartArray[i].url} width=70px height=78px></td>
               <td>￥${cartArray[i].price}</td>
               <td><input type="button" name="minus" value="-" ${cartArray[i].num<=0?'disabled':''}><input type="text" name="amount" value="${cartArray[i].num}" style="width:20px"><input type="button" name="plus" value="+"></td>
               <td class='total'>￥${cartArray[i].singlePrice}</td>
               <td>
                <button>移入收藏</button><br>
                <button class="delete-btn">删除</button>
             </td>
           </tr>
        `
    }
    $('.list').html( `${m} `)
    
}
fu()
//获取各商品总价
function getPrice(){
    for(var i=0;i<cartArray.length;i++){
        cartArray[i].singlePrice=cartArray[i].num*cartArray[i].price
        localStorage.setItem('cart',JSON.stringify(cartArray))
    }
}
getPrice()
// console.log(cartArray);
// var tableEle = document.querySelector('table')
let checkboxAll = document.querySelector('.box')
tableEle.addEventListener('click', function (e) {
    e = e || window.event
    var target = e.target || e.srcElement
    //加一
    if (target.value == '+') {
        var id = target.parentElement.previousElementSibling.previousElementSibling
            .previousElementSibling.previousElementSibling.innerHTML
        var cart = cartArray.find(item=>item.id==id)
        cart.num++
        cart.singlePrice = cart.num * cart.price
        cart.singlePrice = Number(cart.singlePrice.toFixed(2))
        localStorage.setItem('cart',JSON.stringify(cartArray))
        fu()
        total()
    }
    //减一
    if (target.value == '-') {
        let id = target.parentElement.previousElementSibling.previousElementSibling
            .previousElementSibling.previousElementSibling.innerHTML
        let cart = cartArray.find(item=>item.id==id)
        cart.num--
        cart.singlePrice = cart.num * cart.price
        cart.singlePrice = Number(cart.singlePrice.toFixed(2))
        localStorage.setItem('cart',JSON.stringify(cartArray))
        fu()
        total()
    }
    //删除商品
    if (target.innerHTML == "删除") {
        let id = target.parentElement.parentElement.firstElementChild.nextElementSibling.innerHTML
        let index = 0
        // console.log(id);
        for (let i = 0; i < cartArray.length; i++) {
            if (cartArray[i].id == id) {
                index = i
                break
            }
        }
        // console.log(index);
        cartArray.splice(index, 1)
        localStorage.setItem('cart',JSON.stringify(cartArray))
        fu()
    }
    //全选中
    if (target.value == 'check-all') {
        if (checkall) {
            checkall=false
            // for (var i = 0; i < cartArray.length; i++) {
            //     cartArray[i].state = false
            // }
            cartArray.forEach(item=>item.state=false)
        }else{
            checkall=true
            // for (var i = 0; i < cartArray.length; i++) {
            //     cartArray[i].state = true
            // }
            cartArray.forEach(item=>item.state=true)
        }
        console.log(cartArray);
        localStorage.setItem('cart',JSON.stringify(cartArray))
        fu()
        total()
    }
    //单选中映射至全选
    if(target.value=='check'){
        var id=target.parentElement.nextElementSibling.innerHTML
        var cart=cartArray.find(item=>item.id==id)
        cart.state=!cart.state
        checkall=cartArray.every(item=>item.state==true)
        localStorage.setItem('cart',JSON.stringify(cartArray))
        fu()
        total()
    }
})
//求总和
function total() {
    // let sum = cartArray.reduce((s, item) => s += item.singlePrice, 0)
    // spanEle.innerHTML = `￥${sum.toFixed(2)}`

    let sum=0
    for(var i=0;i<cartArray.length;i++){
        if(cartArray[i].state==true){
            sum+=cartArray[i].singlePrice
        }
    }
    $('.total-price').html (`￥${sum.toFixed(2)}`)

}
function back(){
    $('.back').on('click',function(){
        var timer = setInterval(function () {
            document.documentElement.scrollTop=document.documentElement.scrollTop-40
            if(document.documentElement.scrollTop<=0){
                clearInterval(timer)
            }
            },10)
    })
}
back()